package com.qiang.work.test01;

import java.io.*;
import java.net.Socket;

public class Demo0102 {
    public static void main(String[] args) throws IOException {
        //创建客户端
        Socket socket = new Socket("127.0.0.1", 20000);

        //创建本地流来读取本地的图片
        //创建本地文件
        //File f1 = new File("D:\\aa");
        File f1 = new File("D:\\bb");
        File[] f2 = f1.listFiles();
        for (File f3 : f2) {
            String name = f3.getName();
            String sbt = name.substring(name.length() - 3, name.length());
            if (sbt.equals("jpg")) {
                String absolutePath = f3.getAbsolutePath();
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(absolutePath));

                OutputStream os = socket.getOutputStream();
                BufferedOutputStream bos = new BufferedOutputStream(os);
                //刷新
                bos.flush();

                byte[] bb = new byte[2014];
                int len;
                while ((len = bis.read(bb)) != -1) {
                    bos.write(bb, 0, len);
                }

                //给服务器一个结束标记
                socket.shutdownOutput();

                //从网络中获取返回信息
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                bis.close();
                socket.close();
            }
        }


    }
}
